package cn.bearspark.api.service.func.impl;

import cn.bearspark.api.service.func.LikeService;
import cn.bearspark.api.vo.func.like.CommentReqVO;
import cn.bearspark.api.vo.func.like.HappeningReqVO;
import cn.bearspark.comment.rpc.CommentRpc;
import cn.bearspark.common.constant.GlobalConstant;
import cn.bearspark.framework.web.starter.context.BSRequestContext;
import cn.bearspark.happening.rpc.HappeningRpc;
import cn.bearspark.like.rpc.LikeRpc;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author f
 */
@Service
public class LikeServiceImpl implements LikeService {

    @DubboReference(check = GlobalConstant.DUBBO_CHECK)
    private LikeRpc likeRpc;
    @DubboReference(check = GlobalConstant.DUBBO_CHECK)
    private HappeningRpc happeningRpc;
    @DubboReference(check = GlobalConstant.DUBBO_CHECK)
    private CommentRpc commentRpc;

    @Override
    public void likeHappening(HappeningReqVO happeningReqVO) {
        long userId = BSRequestContext.getUserId();
        long happeningId = happeningReqVO.getHappeningId();
        int liked = happeningReqVO.getLiked();

        // 保存/移除点赞记录
        int row = likeRpc.likeHappening(userId, happeningId, liked);

        // 如果操作产生了影响，则增加/减少动态的点赞数
        if (row > 0) {
            happeningRpc.like(userId, happeningId, liked);
        }
    }

    @Override
    public void likeComment(CommentReqVO commentReqVO) {
        long userId = BSRequestContext.getUserId();
        long commentId = commentReqVO.getCommentId();
        int liked = commentReqVO.getLiked();

        // 保存/移除点赞记录
        int row = likeRpc.likeComment(userId, commentId, liked);

        // 如果操作产生了影响，则增加/减少评论的点赞数
        if (row > 0) {
            commentRpc.like(commentId, liked);
        }
    }
}
